/**frame work of string utils
 * @author create by lms 2016.06.25
 * @author update by lms 2018.09.12
 */
package ms.core.tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;

public class StrTool {

	/**
	 * 字符串去首尾空格
	 * @param str
	 * @return
	 */
	public static String trim(String str){
		return (str==null)? "":str.trim();
	}

	/**
	 * 判断字符串是否null或0长度字符串
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str){
		return (str==null) || (str.trim().length()==0);
	}

	/**
	 * 判断字符串是否相同(null和""当作相等处理)
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static boolean equals(String s1, String s2, boolean ignoreCase){
		s1 = (s1==null ? "" : s1);
		s2 = (s2==null ? "" : s2);
		
		return (ignoreCase) ? s1.equalsIgnoreCase(s2) : s1.equals(s2);
	}

	/**
	 * 判断字符串是否空字符串，如果是，则返回指定默认值，否则返回自身
	 * @param str
	 * @param defValue
	 * @return
	 */
	public static String getStrValue(String str, String defValue){
		return isBlank(str)? defValue:str;
	}

	/**
	 * 字符串拼接
	 * @param args
	 * @return
	 */
	public static String concat(String ... args){
		StringBuilder buf = new StringBuilder("");
		for (int i=0; i<args.length; i++)
			buf.append(getStrValue(args[i],""));

		return buf.toString();
	}

	/**
	 * 字符串首字母大写
	 * @param title
	 * @return
	 */
	public static String upperCaseTitle(String title){
		if (isBlank(title)) return title;

		char[] buf = title.toCharArray();

		if ((buf[0]<'a') || (buf[0]>'z'))
			return title;

		buf[0] -= 32;
		return String.valueOf(buf);
	}
	
	/**
	 * 字符串指定位置字母大写
	 * @param title
	 * @param position
	 * @return
	 */
	public static String upperCasePositionTitle(String title, int position){
		if (isBlank(title) || (position>title.length()-1) || (position<0)) return title;

		char[] buf = title.toCharArray();

		if ((buf[position]<'a') || (buf[position]>'z'))
			return title;

		buf[position] -= 32;
		return String.valueOf(buf);
	}

	/**
	 * 字符串指定位置字母小写
	 * @param title
	 * @param position
	 * @return
	 */
	public static String lowerCasePositionTitle(String title, int position){
		if (isBlank(title) || (position>title.length()-1) || (position<0)) return title;

		char[] buf = title.toCharArray();

		if ((buf[position]<'A') || (buf[position]>'Z'))
			return title;

		buf[position] += 32;
		return String.valueOf(buf);
	}

	/**
	 * 字符串首字母小写
	 * @param title
	 * @param position
	 * @return
	 */
	public static String lowerCaseTitle(String title){
		if (isBlank(title)) return title;

		char[] buf = title.toCharArray();
		if ((buf[0]<'A') || (buf[0]>'Z'))
			return title;

		buf[0] += 32;
		return String.valueOf(buf);
	}
	
	/**
	 *to get sub string by lms 2016.07.26 
	 * @param str
	 * @param start start from 0
	 * @param len
	 * @return
	 */
	public static String leftSubString(String str, int start, int len){
		if (isBlank(str)) return str;
		
		try{
			char[] buf = str.toCharArray();
			return String.valueOf(buf, start, len);
		}catch(Exception e){
			Log4j2Tool.error(StrTool.class, "leftSubString:", e);
			return null;
		}
	}

	/**
	 * 从指定字符串右侧起截取指定长度字符串
	 * @param str
	 * @param len
	 * @return
	 */
	public static String rightSubString(String str, int len){
		try{
			char[] buf = str.toCharArray();
			return String.valueOf(buf, str.length()-len, len);
		}catch(Exception e){
			Log4j2Tool.error(StrTool.class, "rightSubString:", e);
			return null;
		}
	}
	
	/**
	 * 从字符串中删除指定位置字符
	 * @param str source string
	 * @param pos 删除位置，0~length-1
	 * @return
	 * @author lms 2016.07.26
	 */
	public static String deleteCharAt(String str, int pos){
		if ((pos>str.length()-1) || (pos<0))
			return str;

		StringBuffer sb = new StringBuffer();
		sb.append(str);
		sb.deleteCharAt(pos);
		return sb.toString();
	}

	/**
	 * 指定字符串分隔
	 * @param content 分隔文本
	 * @param split 分隔符
	 * @return
	 */
	public static List<String> splitString(String content, String split){
		if (split==null || split.length()==0 || isBlank(content))
			return null;

		List<String> tmp = Arrays.asList(content.split(split));
		ArrayList<String> ret = new ArrayList<String>(tmp);
		return ret;
	}


	/**
	 * 逗号分隔的长整型字符串转List<Long>
	 * @param content
	 * @return
	 */
	public static List<Long> splitStringToLongs(String content, String split) {
		if (StrTool.isBlank(content))
			return null;

		return Arrays.stream(content.split(split))
				.map(s -> Long.parseLong(s.trim()))
	            .collect(Collectors.toList());
	}
	
	/**
	 * 逗号分隔的整型字符串转List<Integer>
	 * @param content
	 * @return
	 */
	public static List<Integer> splitStringToIntegers(String content, String split) {
		if (StrTool.isBlank(content))
			return null;

		return Arrays.stream(content.split(split))
				.map(s -> Integer.parseInt(s.trim()))
	            .collect(Collectors.toList());
	}

	/**
	 * List<String> 转字符串
	 * @param list
	 * @param seperator
	 * @return
	 */
	public static String strListToStr(List<String> list, String seperator){
		if (list==null) return null;
		return StringUtils.join(list.toArray(), seperator);
	}

	/**
	 * 字符串转整型
	 * @param v
	 * @param defaultValue
	 * @return
	 */
	public static int strToInt(String v, int defaultValue){
		try{
			if (isBlank(v)) return defaultValue;

			return Integer.parseInt(v.trim());
		}catch (NumberFormatException e){
			Log4j2Tool.error(StrTool.class, "strToInt:", e);
			return defaultValue;
		}
	}

	/**
	 * 字符串转长整型
	 * @param v
	 * @param defaultValue
	 * @return
	 */
	public static long strToLong(String v, long defaultValue){
		try{
			if (isBlank(v)) return defaultValue;

			return Long.parseLong(v.trim());
		}catch (NumberFormatException e){
			Log4j2Tool.error(StrTool.class, "strToLong:", e);
			return defaultValue;
		}
	}

	/**
	 * 16进制字符串转整型，如: "AB"=>171
	 * @param hex
	 * @param defaultValue
	 * @return
	 */
	public static int hexToInt(String hex, int defaultValue){
		try{
			if (isBlank(hex)) return defaultValue;

			return Integer.valueOf(hex.trim(), 16);
		}catch (NumberFormatException e){
			Log4j2Tool.error(StrTool.class, "hexToInt:", e);
			return defaultValue;
		}
	}

    /**
     * 16进制字符串转 long
     * @param hex
     * @return
     */
    public static long hexToLong(String text) {
        if (text.length() == 16) {
            return (hexToLong(text.substring(0, 1))<<60) |
            		hexToLong(text.substring(1));
        }
        return Long.parseLong(text, 16);
    }

    /**
     * 16进制字符串转 double
     * @param hex
     * @return
     */
	public static double hexToDouble(String hex) {
		long longBits = hexToLong(hex);
        return Double.longBitsToDouble(longBits);
	}

	/**
	 * 整型转16禁止字符串, 如: 171=>"AB"
	 * @param v
	 * @param digit
	 * @return
	 */
	public static String intToHex(int v, int digit){
		String ret = Integer.toHexString(v).toUpperCase();
		while (ret.length()<digit) ret = "0"+ret;
		return ret;
	}

	/**
	 * 字符串转 float
	 * @param v
	 * @param defaultValue
	 * @return
	 */
	public static float strToFloat(String v, float defaultValue){
		try{
			if (isBlank(v)) return defaultValue;

			return Float.parseFloat(v.trim());
		}catch (NumberFormatException e){
			Log4j2Tool.error(StrTool.class, "strToFloat:", e);
			return defaultValue;
		}
	}
	
	/**
	 * byte数组转16进制字符串(工控上常用)
	 * @param data
	 * @return the format of string is like "0xA0,0xFE,0x83"
	 */
	public static String bytesToHex(byte[] data, int len){
		String ret="";
		for (int i=0; i<len; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			hex = "0x"+hex;
			if (i==0) ret += hex;
			else ret += ","+hex;
		}
		return ret;
	}

    public static void main(String[] args) {
    	List<Long> list1 = StrTool.splitStringToLongs("1,-12147483648,2,3", ",");
    	System.out.println(list1);
    	
    	String str = "$11.7";
    	System.out.println(Double.parseDouble(str.replaceAll("\\$", "")));
    }
}
